/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

/**
 * @author L. Guerin
 *
 */
public class SysUtil
{
    private static final String[] VOID_STRING_ARRAY = new String[0];
    
    /**
     * Returns the Operating System name
     * @return "os.name" property value
     */
    public static String getOSName()
    {
        return System.getProperty("os.name");
    }

    /**
     * Returns the Operating System version
     * @return "os.version" property value
     */
    public static String getOSVersion()
    {
        return System.getProperty("os.version");
    }

    /**
     * Returns the Operating System user name
     * @return "user.name" property value
     */
    public static String getUserName()
    {
        return System.getProperty("user.name");
    }

    /**
     * Destroy the given process
     * @param process
     */
    private static void destroyProcess( Process process )
    {        
        if ( process != null )
        {
            //--- Close process error stream
            try {
                process.getErrorStream().close();
            }
            catch (IOException ex) {}
            //--- Close process output stream
            try {
                process.getOutputStream().close();
            }
            catch (IOException ex) {}

            //--- Destroy the process
            process.destroy();
        }
    }
    
    /**
     * Executes the given command with its arguments
     * @param cmdAndArgs the arguments for the 'exec' method of the 'Process' instance
     * @return the execution output ( 0 to N lines )
     */
    public static String[] exec( String[] cmdAndArgs )
    {
        LinkedList<String> processOutput = null ;
        Runtime runtime = Runtime.getRuntime();
        Process process = null ;
        BufferedReader in = null ;
        try
        {
            process = runtime.exec(cmdAndArgs);
            if (process != null) 
            {
                processOutput = new LinkedList<String>();
                in = new BufferedReader(new InputStreamReader(process.getInputStream()), 128);
                String line = null;
                while ((line = in.readLine()) != null)  // Under Windows : one void line after each line ! ( due to CR-LF ? )
                {
                    processOutput.add(line);
                }
                in.close();
                //process.destroy();
            }
        } catch (IOException e)
        {
            throw new RuntimeException("Error in exec(" + cmdAndArgs[0] + ",...) : IOException ",e);
        }
        finally
        {
            if (in != null) 
            {
                try { 
                    in.close();
                } catch (IOException ex) {}
            }
            destroyProcess( process );
        }
        
        if ( processOutput != null )
        {
            // return (String[]) processOutput.toArray(); // Exception            
            int n = processOutput.size() ;
            String[] lines = new String[n];
            for ( int i = 0 ; i < n ; i++ )
            {
                lines[i] = (String) processOutput.get(i);
            }
            return lines ;
        }
        else
        {
            return VOID_STRING_ARRAY ;
        }
    }
    
}
